// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// The ModelAgent is a simple consumer agent that prepares data coming from a agent for display (but doesn't display it). 
    /// The data is stored on a data-bindable model that views can bind to. 
    /// Unlike a published state, the model instance remains the same for the lifetime of the model agent.
    /// </summary>
    /// <remarks>This class doesn't derive from ConsumerAgent because it must not activate/deactivate the agent it subscribes to.</remarks>
    /// <typeparam name="TMessage">The state generated by the upstream agent.</typeparam>
    /// <typeparam name="TModel">The model maintained by this class.</typeparam>
    [DataContract]
    public abstract class ModelAgent<TMessage, TModel> : ModelAgentBase<TMessage, TModel>, ISubscriptionReceiver<TMessage>
        where TMessage : AgentMessage
        where TModel : Model, new()
    {
        /// <summary>
        /// The name of the agent we subscribe to.
        /// </summary>
        [DataMember(Name = "Producer")]
        private readonly string producerAgentName = null;

        /// <summary>
        /// The agent we subscribe to.
        /// </summary>
        private IAgentHost producerAgent;

        /// <summary>
        /// Initializes a new instance of the <see cref="ModelAgent{TMessage,TModel}"/> class. 
        /// Initializes a new instance of the ModelAgent class
        /// </summary>
        /// <param name="name">
        /// Agent name
        /// </param>
        /// <param name="producerAgentName">
        /// The name of the agent we subscribe to.
        /// </param>
        /// <param name="maxUpdateIntervalMilliseconds">
        /// The interval on which to issue data binding updates to the UI
        /// </param>
        public ModelAgent(string name, string producerAgentName, int maxUpdateIntervalMilliseconds)
            : base(name, maxUpdateIntervalMilliseconds)
        {
            this.producerAgentName = producerAgentName;
        }

        /// <summary>
        /// Gets the agent we subscribe to.
        /// </summary>
        protected IAgentHost ProducerAgent
        {
            get
            {
                return this.producerAgent;
            }
        }

        /// <summary>
        /// Called to finish initialization after the constructor has finished.
        /// </summary>
        /// <param name="locator">Locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            // subscribe to the upstream agent
            this.producerAgent = this.Locator.GetAgent(this.producerAgentName);
            this.ProducerAgent.Subscribe(this);
        }
    }
}
